import java.util.Comparator;
import java.util.Iterator;
import java.util.Objects;
import java.util.Vector;
import java.util.function.Consumer;

public class Student {
   private String name;

   private String garden;
   private int age;
   private double score;

    public Student(String name, String garden, int age, double score) {
        this.name = name;
        this.garden = garden;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGarden() {
        return garden;
    }

    public void setGarden(String garden) {
        this.garden = garden;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "姓名='" + name + '\'' +
                ", 性别='" + garden + '\'' +
                ", 年龄=" + age +
                ", 成绩=" + score +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Double.compare(score, student.score) == 0 && Objects.equals(name, student.name) && Objects.equals(garden, student.garden);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, garden, age, score);
    }
}
class Test {
    public static void main(String[] args) {
        Vector vector = new Vector();
        vector.add(new Student("zcj", "男", 20, 88.8));
        vector.add(new Student("abc", "女", 18, 648));
        vector.add(new Student("ghx", "男", 24, 188));
        vector.add(new Student("wxy", "女", 35, 98));
        vector.add(new Student("wyn", "男", 12, 328));
        vector.add(new Student("zyn", "女", 36, 30));


//        System.out.println("冒泡比较姓名");
//        MaoSort(vector);
//        System.out.println("for循环输出");
//        for (Object o:vector
//             ) {
//            System.out.println(o);
//        }
//
//
//        System.out.println("选择排序年龄");
//        ChangSort(vector);
//        System.out.println("迭代器输出");
//        Iterator iterator = vector.iterator();
//        while (iterator.hasNext()) {
//            System.out.println(iterator.next());
//        }
//
//
//        System.out.println("重写接口compaper的方法比较年龄");
//        vector.sort(new StudentCompaper());
//        System.out.println("lambda表达式输出");
//        vector.forEach(o -> System.out.println(o));
//
//
//        System.out.println("匿名类重写Compaper方法比较成绩");
//        vector.sort(new Comparator() {
//            @Override
//            public int compare(Object o1, Object o2) {
//                if (o1 instanceof Student stu1 && o2 instanceof Student stu2){
//                    return (int) (stu1.getScore() - stu2.getScore());
//                }
//                return 0;
//            }
//        });
//        System.out.println("lambda表达式输出");
//        vector.forEach(o -> System.out.println(o));


        System.out.println("插入排序年龄");
        ChaRuSort(vector);
        System.out.println("forEach输出");
        vector.forEach(o -> System.out.println(o));
//        vector.forEach(new Consumer() {
//            @Override
//            public void accept(Object o) {
//                System.out.println(o);
////                if (o instanceof Student stu) {
////                    if (stu.getName().equals("abc")) {
////                        vector.remove(o);
////                    }
////                }
//            }
//        });

    }


    //冒泡排序姓名
    public static void MaoSort(Vector vector) {
        for (int i = 0; i < vector.size() - 1; i++) {
            for (int j = 0; j < vector.size() - 1 - i; j++) {
                Student stu1 = (Student) vector.get(j);
                Student stu2 = (Student) vector.get(j + 1);
                if (stu1.getName().compareTo(stu2.getName()) > 0) {
                    vector.set(j, stu2);
                    vector.set(j + 1, stu1);
                }
            }
        }
    }

    //选择排序年龄
    public static void ChangSort(Vector vector) {
        for (int i = 0; i < vector.size(); i++) {
            int minindex = i;
            Student stu1 = (Student) vector.get(minindex);
            for (int j = 0; j < vector.size(); j++) {
                Student stu2 = (Student) vector.get(j);
                if (stu1.getAge() > stu2.getAge()) {
                    minindex = j;
                }
            }
            Student stu = (Student) vector.get(i);
            vector.set(minindex, stu1);
            vector.set(i, stu);
        }
    }

    //插入排序成绩
    public static void ChaRuSort(Vector vector) {
        for (int i = 0; i < vector.size() - 1; i++) {
            Student current = (Student) vector.get(i + 1);
            int index = i;
            Student student = (Student) vector.get(index);
            while (index >= 0 && current.getScore() < student.getScore()) {
                vector.set(index + 1, student);
                index--;
            }
            vector.set(index + 1, current);
        }
    }
}
//比较器
class StudentCompaper implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        if (o1 instanceof Student stu1){
            if (o2 instanceof Student stu2){
                return stu1.getAge() - stu2.getAge();
            }
        }
        return 0;
    }
}
